//todo: 用于本地通知的显示，我们需要初始化并配置它来支持各种类型的通知
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import '../models/notification_model.dart';

class PushNotificationService {
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  // 初始化通知插件
  Future<void> initialize() async {
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('app_icon');

    const InitializationSettings initializationSettings =
        InitializationSettings(android: initializationSettingsAndroid);

    await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  // 创建通知渠道
  Future<void> createNotificationChannel() async {
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      'default_channel',
      'Default Notifications',
      description: 'This is the default channel for notifications',
      importance: Importance.high,
      playSound: true,
    );

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
  }

  // 根据通知类型决定不同的显示方式
  Future<void> showNotification(NotificationModel notification) async {
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      'default_channel',
      'Default notifications',
      importance: Importance.high,
      priority: Priority.high,
      playSound: true,
      // style: NotificationStyle.inbox,
    );

    const NotificationDetails platformChannelSpecifics =
        NotificationDetails(android: androidPlatformChannelSpecifics);

    switch (notification.type) {
      case NotificationType.chat:
        await flutterLocalNotificationsPlugin.show(
          0,
          notification.title,
          notification.body,
          platformChannelSpecifics,
          payload: notification.payload,
        );
        break;
      case NotificationType.app:
        await flutterLocalNotificationsPlugin.show(
          1,
          notification.title,
          notification.body,
          platformChannelSpecifics,
          payload: notification.payload,
        );
        break;
      case NotificationType.system:
        await flutterLocalNotificationsPlugin.show(
          2,
          notification.title,
          notification.body,
          platformChannelSpecifics,
          payload: notification.payload,
        );
        break;
      case NotificationType.promotional:
        await flutterLocalNotificationsPlugin.show(
          3,
          notification.title,
          notification.body,
          platformChannelSpecifics,
          payload: notification.payload,
        );
        break;
    }
  }

  // 删除通知
  Future<void> cancelNotification(int notificationId) async {
    await flutterLocalNotificationsPlugin.cancel(notificationId);
  }
}
